package com.yanqu.road.server.cmd.strongholdwar.wild;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.entity.activity.stronghold.StrongholdWildUserData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdMgr;
import com.yanqu.road.server.pb.CrossStrongholdPb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.C_CROSS_STRONGHOLD_WAR_DISPATCH_WILD_PATRONS, desc = "派遣门客到野外据点")
public class CrossDispatchPatronsWildCmd extends CrossUnionCommand {
    private int wildId;
    private UserPatrons patrons;
    private UserBaseInfo userBaseInfo;
    private int activityId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        StrongholdWarProto.DispatchPatronsToCrossWildReqMsg reqMsg = StrongholdWarProto.DispatchPatronsToCrossWildReqMsg.parseFrom(packet.getBytes());
        wildId = reqMsg.getWildId();
        String patronsStr = reqMsg.getPatronsStr();
        patrons = JSON.parseObject(patronsStr, UserPatrons.class);
        PlayerProto.PlayerBaseTempMsg playerData = reqMsg.getPlayerData();
        userBaseInfo = PlayerBasePb.parseBaseUserInfo(playerData);
        activityId = reqMsg.getActivityId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ret = CrossStrongholdMgr.dispatchPatrons(activityId, wildId, serverId, packet.getPlayerId(), userBaseInfo, patrons);
        StrongholdWarProto.DispatchPatronsToCrossWildRespMsg.Builder resp = StrongholdWarProto.DispatchPatronsToCrossWildRespMsg.newBuilder();
        resp.setRet(ret);
        StrongholdWildUserData wildUser = CrossStrongholdMgr.getWildUser(activityId, serverId, packet.getPlayerId());
        //派遣门客信息
        if (wildUser != null) {
            StrongholdWarProto.StrongholdDispatchPatronsTempMsg.Builder wildUserMsg = CrossStrongholdPb.getWildUserMsg(wildUser);
            resp.setPatrons(wildUserMsg);
        }
        resp.setPatronsId(patrons.getPatronsId());
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_STRONGHOLD_WILD_DISPATCH, resp);
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), pbMsg);
    }

    @Override
    public void sendData() {

    }
}
